﻿using System;
using System.Drawing;

namespace Oscilloscope.ChannelBuffer
{
	// Token: 0x02000027 RID: 39
	internal class MultipleDigitalChannelBufferDataIterator : IChannelBufferDataIterator
	{
		// Token: 0x0600012A RID: 298 RVA: 0x00009478 File Offset: 0x00007678
		public void Initialize(ChannelBufferData[] dataBuffer, int dataBufferValidCount, double dataBufferStartTime, double timeInterval, int bitIndex)
		{
			if (bitIndex < 0 || bitIndex > 15)
			{
				throw new ArgumentOutOfRangeException("bitIndex");
			}
			this.iteratorCurrentIndex = 0;
			this.iteratorCurrentTime = dataBufferStartTime;
			this.iteratorTimeInterval = timeInterval;
			this.dataBufferValidCount = dataBufferValidCount;
			this.dataBuffer = dataBuffer;
			this.preValidData = -1.0;
			this.hasPreValidData = false;
			this.hasDataPadding = false;
			this.bitIndex = bitIndex;
		}

		// Token: 0x0600012B RID: 299 RVA: 0x000094E4 File Offset: 0x000076E4
		private double GetBitData(int bufferIndex)
		{
			ushort num = (ushort)this.dataBuffer[bufferIndex].Data;
			return (double)(num >> this.bitIndex & 1);
		}

		// Token: 0x0600012C RID: 300 RVA: 0x00009514 File Offset: 0x00007714
		public bool Next(ref double value, ref double time)
		{
			while (this.iteratorCurrentIndex < this.dataBufferValidCount)
			{
				if (this.dataBuffer[this.iteratorCurrentIndex].IsValid)
				{
					if (!this.hasPreValidData)
					{
						value = this.GetBitData(this.iteratorCurrentIndex);
						time = this.iteratorCurrentTime;
						this.preValidData = this.GetBitData(this.iteratorCurrentIndex);
						this.hasPreValidData = true;
						this.hasDataPadding = false;
						this.iteratorCurrentIndex++;
						this.iteratorCurrentTime += this.iteratorTimeInterval;
						return true;
					}
					if (this.GetBitData(this.iteratorCurrentIndex) != this.preValidData)
					{
						if (this.hasDataPadding)
						{
							value = this.GetBitData(this.iteratorCurrentIndex);
							time = this.iteratorCurrentTime;
							this.preValidData = this.GetBitData(this.iteratorCurrentIndex);
							this.hasDataPadding = false;
							this.iteratorCurrentIndex++;
							this.iteratorCurrentTime += this.iteratorTimeInterval;
							return true;
						}
						value = this.preValidData;
						time = this.iteratorCurrentTime;
						this.hasDataPadding = true;
						return true;
					}
					else if (this.iteratorCurrentIndex == this.dataBufferValidCount - 1)
					{
						value = this.preValidData;
						time = this.iteratorCurrentTime;
						this.iteratorCurrentIndex++;
						this.iteratorCurrentTime += this.iteratorTimeInterval;
						return true;
					}
				}
				this.iteratorCurrentIndex++;
				this.iteratorCurrentTime += this.iteratorTimeInterval;
			}
			return false;
		}

		// Token: 0x0600012D RID: 301 RVA: 0x000096A0 File Offset: 0x000078A0
		public bool PeekNext(ref double value, ref double time)
		{
			double num = this.iteratorCurrentTime;
			for (int i = this.iteratorCurrentIndex; i < this.dataBufferValidCount; i++)
			{
				if (this.dataBuffer[i].IsValid)
				{
					if (!this.hasPreValidData)
					{
						value = this.GetBitData(i);
						time = num;
						return true;
					}
					if (this.GetBitData(i) != this.preValidData)
					{
						if (this.hasDataPadding)
						{
							value = this.GetBitData(i);
							time = num;
							return true;
						}
						value = this.preValidData;
						time = num;
						return true;
					}
					else if (i == this.dataBufferValidCount - 1)
					{
						value = this.preValidData;
						time = num;
						return true;
					}
				}
				num += this.iteratorTimeInterval;
			}
			return false;
		}

		// Token: 0x0600012E RID: 302 RVA: 0x00009750 File Offset: 0x00007950
		public PointF[] SetNewPoint(float x, float y)
		{
			return new PointF[]
			{
				new PointF(x, y)
			};
		}

		// Token: 0x0600012F RID: 303 RVA: 0x00009778 File Offset: 0x00007978
		public PointF[] GetPoint()
		{
			return null;
		}

		// Token: 0x04000089 RID: 137
		private int iteratorCurrentIndex;

		// Token: 0x0400008A RID: 138
		private double iteratorCurrentTime;

		// Token: 0x0400008B RID: 139
		private double iteratorTimeInterval;

		// Token: 0x0400008C RID: 140
		private int dataBufferValidCount;

		// Token: 0x0400008D RID: 141
		private ChannelBufferData[] dataBuffer;

		// Token: 0x0400008E RID: 142
		private double preValidData;

		// Token: 0x0400008F RID: 143
		private bool hasPreValidData;

		// Token: 0x04000090 RID: 144
		private bool hasDataPadding;

		// Token: 0x04000091 RID: 145
		private int bitIndex;
	}
}
